home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_9.zip / LIBW.C < prev    next >
C/C++ Source or Header  |  1993-07-27  |  43KB  |  1,504 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9.  
  10. /* libw - procedures for writing (in C format) ais and tre files*/
  11.  
  12. #ifdef __GNUG__
  13. extern "C"
  14. {
  15. #include <sys/types.h>
  16. #include <sys/dir.h>
  17. }
  18. #endif
  19. #include "hdr.h"
  20. #include "vars.h"
  21. #include "libhdr.h"
  22. #include "ifile.h"
  23. #include "setp.h"
  24. #include "dbxp.h"
  25. #include "miscp.h"
  26. #include "smiscp.h"
  27. #include "chapp.h"
  28. #include "libp.h"
  29. #include "libfp.h"
  30. #include "libwp.h"
  31.  
  32. #ifdef BSD
  33. /* Needed for cleanup_files routine */
  34. #include <sys/types.h>
  35. #include <sys/dir.h>
  36. #endif
  37.  
  38. #ifdef SYSTEM_V
  39. /* Needed for cleanup_files routine */
  40. #include <fcntl.h>
  41. #include <sys/types.h>
  42. #include <sys/dir.h>
  43. #endif
  44.  
  45. #ifdef IBM_PC
  46. #include <dos.h>
  47. #include <errno.h>
  48. #endif
  49.  
  50. extern char *LIBRARY_PREFIX;
  51. extern IFILE *TREFILE, *AISFILE, *STUBFILE, *LIBFILE;
  52.  
  53. static void putdcl(IFILE *, Declaredmap);
  54. static void putlitmap(IFILE *, Symbol);
  55. static void putnod(IFILE *, char *, Node);
  56. static void putnodref(IFILE *, char *, Node);
  57. static void putint(IFILE *, char *, int );
  58. static void putlong(IFILE *, char *, long);
  59. static void putmisc(IFILE *, Symbol);
  60. static void putrepr(IFILE *, Symbol);
  61. static void putunt(IFILE *, char *, unsigned int);
  62. static void putnval(IFILE *, Node);
  63. static void putuint(IFILE *, char *, int *);
  64. static void putovl(IFILE *, Symbol);
  65. static void putsig(IFILE *, Symbol);
  66. static void putsym(IFILE *, char *, Symbol);
  67. static void putudecl(IFILE *, int);
  68. static long write_next(IFILE *);
  69. static void put_unit_unam(IFILE *, Symbol);
  70.  
  71. static void putdcl(IFILE *ofile, Declaredmap d)                        /*;putdcl*/
  72. {
  73.     Fordeclared fd;
  74.     char    *id;
  75.     Symbol    sym;
  76.     int        i, n = 0;
  77.     typedef struct {
  78.         char *iden;
  79.         short sym_seq;
  80.         short sym_unit;
  81.         short visible;
  82.     }f_dmap_s;
  83.     f_dmap_s ** dptrs;
  84.     f_dmap_s *    filedmap;
  85.     f_dmap_s *    save_filedmap;
  86.  
  87.     if (d == (Declaredmap)0) {
  88.         putnum(ofile, "putdcl-is-map-defined", 0);
  89.         return;
  90.     }
  91.     putnum(ofile, "putdcl-is-map-defined", 1); /* to indicate map defined */
  92.     n = 0; /* count number of entries where defined */
  93.     FORDECLARED(id, sym, d, fd);
  94.         n += 1;
  95.     ENDFORDECLARED(fd);
  96.     putnum(ofile, "putdcl-number-defined", n);
  97.     if (n == 0) return;
  98.     save_filedmap = filedmap = (f_dmap_s *)
  99.       ecalloct((unsigned)n, sizeof(f_dmap_s), "put-dcl-save-filedmap");
  100.     dptrs =
  101.       (f_dmap_s **) emalloct(sizeof(f_dmap_s *) * (unsigned)n, "put-dcl-dptrs");
  102.     n = 0;
  103.     FORDECLARED(id, sym, d, fd);
  104.         n++;  /* number of entries seen so far */
  105.         filedmap->iden = id;
  106.         if (sym == (Symbol) 0)
  107.             filedmap->sym_seq = filedmap->sym_unit = 0;
  108.         else {
  109.             filedmap->sym_seq = S_SEQ(sym);
  110.             filedmap->sym_unit = S_UNIT(sym);
  111.         }
  112.         filedmap->visible = IS_VISIBLE(fd);
  113.         /* now, insert pointer to new record such that ids are sorted 
  114.          * this is necessary (for debugging only!) to ensure entries appear
  115.          * in the same order each time the declared map is written
  116.          */
  117.         i = n-1;
  118.         while ( i > 0 && strcmp(filedmap->iden, dptrs[i-1]->iden) < 0) {
  119.             dptrs[i] = dptrs[i-1];
  120.             i--;
  121.         }
  122.         dptrs[i] = filedmap;
  123.         filedmap++;
  124.     ENDFORDECLARED(fd);
  125.  
  126.     /* now, write to file */
  127.     for (i = 0; i < n; i++ ) {
  128.         putstr(ofile, "str", dptrs[i]->iden);
  129.         putnum(ofile, "seq", dptrs[i]->sym_seq);
  130.         putnum(ofile, "unt", dptrs[i]->sym_unit);
  131.         putnum(ofile, "vis", dptrs[i]->visible);
  132.     }
  133.     efreet((char *)save_filedmap, "putdcl-save-filedmap");
  134.     efreet((char *) dptrs, "putdcl-dptrs");
  135. }
  136.  
  137. static void putlitmap(IFILE *ofile, Symbol sym)                /*;putlitmap*/
  138. {
  139.     /* called for na_enum to output literal map.
  140.      * The literal map is a tuple, entries consisting of string followed
  141.      * by integer.
  142.      */
  143.  
  144.     Tuple    tup;
  145.     int i, n;
  146.  
  147.     tup = (Tuple) OVERLOADS(sym);
  148.     n = tup_size(tup);
  149.     putnum(ofile, "litmap-n", n);
  150.     for (i = 1; i <= n; i += 2) {
  151.         putstr(ofile, "litmap-str", tup[i]);
  152.         putnum(ofile, "litmap-value", (int) tup[i+1]);
  153.     }
  154. }
  155.  
  156. static void putnod(IFILE *ofile, char *desc, Node node)                /*;putnod*/
  157. {
  158.     /* Write information for the node to a file (ofile)
  159.      * Since all the nodes in the tree all have the same N_UNIT value, 
  160.      * the node can be written to the file in a more compact format.
  161.      * The N_UNIT of the node itself and of its children (N_AST1...) need not
  162.      * be written out only their N_SEQ filed needs to be written out. There
  163.      * is one complication of this scheme. OPT_NODE which is (seq=1,unit=0) will
  164.      * conflict with (seq=1,unit=X)  of current unit. Therefore, in this case a 
  165.      * sequence # of -1 will signify OPT_NODE.
  166.      */
  167.  
  168.     Tuple    tup;
  169.     Fortup    ft1;
  170.     int     has_n_list = 0;
  171.     int        nk;
  172.     Node    nod;
  173.     short    fnum[24];
  174.     int        fnums = 0;
  175.     Symbol    sym;
  176.  
  177. #ifdef DEBUG
  178.     if (trapns>0 && N_SEQ(node) == trapns && N_UNIT(node) == trapnu)trapn(node);
  179. #endif
  180.     /* copy standard info */
  181.     nk = N_KIND(node);
  182.     fnum[fnums++] = nk;
  183.     fnum[fnums++] = N_SEQ(node);
  184.     if (N_LIST_DEFINED(nk)) {
  185.         tup = N_LIST(node);
  186.         if (tup == (Tuple)0) 
  187.             has_n_list = 0;
  188.         else
  189.             has_n_list = 1 + tup_size(tup);
  190.         fnum[fnums++] = has_n_list;
  191.     }
  192.     /* ast fields */
  193.     /* See comment above for description of compact format.*/
  194.     if (N_AST1_DEFINED(nk)) {
  195.         nod = N_AST1(node);
  196.         fnum[fnums++] = (N_UNIT(nod) != 0) ? N_SEQ(nod) : -1;
  197.     }
  198.     if (N_AST2_DEFINED(nk)) {
  199.         nod = N_AST2(node);
  200.         fnum[fnums++] = (N_UNIT(nod) != 0) ? N_SEQ(nod) : -1;
  201.     }
  202.     if (N_AST3_DEFINED(nk)) {
  203.         nod = N_AST3(node);
  204.         fnum[fnums++] = (N_UNIT(nod) != 0) ? N_SEQ(nod) : -1;
  205.     }
  206.     if (N_AST4_DEFINED(nk)) {
  207.         nod = N_AST4(node);
  208.         fnum[fnums++] = (N_UNIT(nod) != 0) ? N_SEQ(nod) : -1;
  209.     }
  210.     /*fnum[fnums++] = N_SIDE(node);*/
  211.     /* N_UNQ only if defined */
  212.     if (N_UNQ_DEFINED(nk))  {
  213.         sym = N_UNQ(node);
  214.         fnum[fnums++] = (sym != (Symbol)0) ? S_SEQ(sym) : 0;
  215.         fnum[fnums++] = (sym != (Symbol)0) ? S_UNIT(sym) : 0;
  216.     }
  217.     if (N_TYPE_DEFINED(nk)) {
  218.         sym = N_TYPE(node);
  219.         fnum[fnums++] = (sym != (Symbol)0) ? S_SEQ(sym) : 0;
  220.         fnum[fnums++] = (sym != (Symbol)0) ? S_UNIT(sym) : 0;
  221.     }
  222.     /* write fnums followed by fnum info as array */
  223.  
  224.     putnum(ofile, desc, fnums);
  225.     /*fwrite((char *) &fnums, sizeof(short), 1, ofile->fh_file);*/
  226.     fwrite((char *) fnum, sizeof(short), fnums, ofile->fh_file);
  227.  
  228.     /* write out n_list if needed */
  229.     if (has_n_list>1) {
  230.         tup = N_LIST(node);
  231.         FORTUP(nod = (Node), tup, ft1);
  232.             putnodref(ofile, "n-list-nodref", nod);
  233.         ENDFORTUP(ft1);
  234.     }
  235.     if (N_VAL_DEFINED(nk)) {
  236.         putnval(ofile, node);
  237.     }
  238. }
  239.  
  240. static void putnodref(IFILE *ofile, char *desc, Node node)        /*;putnodref*/
  241. {
  242.     /* OPT_NODE is node in unit 0 with sequence 1, and needs
  243.      * no special handling here
  244.      */
  245.  
  246.     if (node == (Node)0) {
  247.         putpos(ofile, "nref-seq", 0);
  248.         putunt(ofile, "nref-unt", 0);
  249.     }
  250.     else {
  251.         putpos(ofile, "nref-seq", N_SEQ(node));
  252.         putunt(ofile, "nref-unt", N_UNIT(node));
  253.     }
  254. }
  255.  
  256. static void putint(IFILE *ofile, char *desc, int n)                /*;putint*/
  257. {
  258.     /* write int to output file */
  259.  
  260.     int s = n;
  261.  
  262.     fwrite((char *) &s, sizeof(int), 1, ofile->fh_file);
  263. }
  264.  
  265. static void putlong(IFILE *ofile, char *desc, long n)                /*;putlong*/
  266. {
  267.     /* write long to output file */
  268.  
  269.     long s = n;
  270.  
  271.     fwrite((char *) &s, sizeof(long), 1, ofile->fh_file);
  272. }
  273.  
  274. static void putmisc(IFILE *ofile, Symbol sym)                /*;putmisc*/
  275. {
  276.     /* write out MISC information if present 
  277.      * MISC is integer except for package, in which case it is a triple.
  278.      * The first two components are integers, the last is  a tuple of
  279.      * symbols
  280.      */
  281.  
  282.     int    nat, i, n;
  283.     char   *m;
  284.     Tuple tup;
  285.  
  286.     nat = NATURE(sym);
  287.     m = MISC(sym);
  288.     if ((nat == na_package || nat == na_package_spec )&& m != (char *)0) {
  289.         tup = (Tuple) m;
  290.         putnum(ofile, "misc-package-1", (int)tup[1]);
  291.         putnum(ofile, "misc-package-2", (int)tup[2]);
  292.         tup = (Tuple) tup[3];
  293.         n = tup_size(tup);
  294.         putnum(ofile, "misc-package-tupsize", n);
  295.         for (i = 1; i <= n; i++)
  296.             putsymref(ofile, "misc-package-symref", (Symbol) tup[i]);
  297.     }
  298.     else if ((nat == na_procedure || nat == na_function) && m != (char *)0) {
  299.         /* misc is tuple. first entry is string, second is symbol */
  300.         tup = (Tuple) m;
  301.         putnum(ofile, "misc-number", (int) tup[1]);
  302.         putsymref(ofile, "misc-symref", (Symbol) tup[2]);
  303.     }
  304.     else {
  305.         putnum(ofile, "misc", (int)m);
  306.     }
  307. }
  308.  
  309. static void putrepr(IFILE *ofile, Symbol sym)                /*;putrepr*/
  310. {
  311.     /* write out representation